	#   2010-11-17:   NOT USED ANYMORE !!!!!!!    
	# 
	# to delete in the future.....
	 






from django import template
from fb_utils.utils import djfacetlog

register = template.Library()


# ===>  if we keep this approach, we might just remove the little formatting code in ..._inner.html 
@register.inclusion_tag('djfacet/snippet_mpttfacet_inner.html')
def render_tree(facetvalues, facet, facetgroup):
	
	if False:  # testing 
		stuff = []
		for x in facetvalues:
			stuff.append(facet.recursive_tree_forfacetvalue(x))

	if True:
		stringa = ""
		#  the dict cointains the tree structure with IDs only
		djfacetlog("\n STARTING LOADING THE TREE............... \n")
		mydict = facet.recursive_tree_forfacetvalue_list(facetvalues)
		djfacetlog("\n DONE LOADING THE TREE \n")
		# stringa ideally should be built inside the template, using a recursive model.. TODO
		djfacetlog("\n STARTING VISUALIZING THE TREE............... \n")
		stringa = tree_visualizer(0, mydict, "", facet, facetgroup, facetvalues)
		djfacetlog("\n DONE VISUALIZING THE TREE \n")			
	return { 
			'stringa' : stringa,
			'facet_id' : facet.id, 
			
			# 'stuff' : stuff,		
			# 'facet' : facet, 			
			# 'facetvalues': facetvalues, 
			# 'facetgroup' : facetgroup
			}



# once we obtained the tree_dict with the IDs, here we check again that only the values with updated howmany are passed
# OPTIMIZE? ? ? change the method in facet.py ? 

def tree_visualizer(root, tree_dict, res, facet, facetgroup, facetvalues):
	for x in tree_dict[root]:
		facetvalue = [v for v in facetvalues if v.id == x]
		if facetvalue:
			res += """<li style="font-size:12px;"><a id="%s_%d" class="%s" >%s (%d)</a>""" % (facetgroup.uniquename, 
				facetvalue[0].id, facet.name, facetvalue[0].displayname,  facetvalue[0].howmany)
		else:
			facetvalue = facet.get_facetvalue_from_id(x)
			res += """<li>%s""" % facetvalue.displayname  #<span style="color: #2C528B;">%s</span>
		if tree_dict.get(x):
			# djfacetlog("noise")
			res += "<ul>" + tree_visualizer(x, tree_dict, '', facet, facetgroup, facetvalues) + "</ul>"
		res += "</li>"
	return res
	
	
	# 
	# 	
	# def render_tree_string(facetvalues, facet, facetgroup):
	# 	if True:
	# 		stringa = ""
	# 		#  the dict cointains the tree structure with IDs only
	# 		djfacetlog("\n STARTING LOADING THE TREE............... \n")
	# 		mydict = facet.recursive_tree_forfacetvalue_list(facetvalues)
	# 		djfacetlog("\n DONE LOADING THE TREE \n")
	# 		# stringa ideally should be built inside the template, using a recursive model.. TODO
	# 		djfacetlog("\n STARTING VISUALIZING THE TREE............... \n")
	# 		stringa = tree_visualizer(0, mydict, "", facet, facetgroup, facetvalues)
	# 		djfacetlog("\n DONE VISUALIZING THE TREE \n")			
	# 	return stringa 
	# 	
	# 	
	# 	
	
	

